<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="学年学期" prop="academicYear">
        <el-input
          v-model="queryParams.academicYear"
          placeholder="请输入学年学期"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="补重学期" prop="retakeSemester">
        <el-input
          v-model="queryParams.retakeSemester"
          placeholder="请输入补重学期"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="课程编号" prop="courseCode">
        <el-input
          v-model="queryParams.courseCode"
          placeholder="请输入课程编号"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="课程名称" prop="courseName">
        <el-input
          v-model="queryParams.courseName"
          placeholder="请输入课程名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="总成绩" prop="totalScore">
        <el-input
          v-model="queryParams.totalScore"
          placeholder="请输入总成绩"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="平时成绩" prop="regularScore">
        <el-input
          v-model="queryParams.regularScore"
          placeholder="请输入平时成绩"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="期中成绩" prop="midtermScore">
        <el-input
          v-model="queryParams.midtermScore"
          placeholder="请输入期中成绩"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="实验成绩" prop="labScore">
        <el-input
          v-model="queryParams.labScore"
          placeholder="请输入实验成绩"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="期末成绩" prop="finalScore">
        <el-input
          v-model="queryParams.finalScore"
          placeholder="请输入期末成绩"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="辅修成绩" prop="minorScore">
        <el-input
          v-model="queryParams.minorScore"
          placeholder="请输入辅修成绩"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="绩点" prop="gpa">
        <el-input
          v-model="queryParams.gpa"
          placeholder="请输入绩点"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="成绩标志" prop="gradeFlag">
        <el-input
          v-model="queryParams.gradeFlag"
          placeholder="请输入成绩标志"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="总学时" prop="totalHours">
        <el-input
          v-model="queryParams.totalHours"
          placeholder="请输入总学时"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="学分" prop="credit">
        <el-input
          v-model="queryParams.credit"
          placeholder="请输入学分"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="开课单位" prop="department">
        <el-input
          v-model="queryParams.department"
          placeholder="请输入开课单位"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="开课系" prop="faculty">
        <el-input
          v-model="queryParams.faculty"
          placeholder="请输入开课系"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['system:grade:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['system:grade:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['system:grade:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['system:grade:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="gradeList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="课程唯一ID" align="center" prop="courseId" />
      <el-table-column label="学年学期" align="center" prop="academicYear" />
      <el-table-column label="补重学期" align="center" prop="retakeSemester" />
      <el-table-column label="课程编号" align="center" prop="courseCode" />
      <el-table-column label="课程名称" align="center" prop="courseName" />
      <el-table-column label="总成绩" align="center" prop="totalScore" />
      <el-table-column label="平时成绩" align="center" prop="regularScore" />
      <el-table-column label="期中成绩" align="center" prop="midtermScore" />
      <el-table-column label="实验成绩" align="center" prop="labScore" />
      <el-table-column label="期末成绩" align="center" prop="finalScore" />
      <el-table-column label="辅修成绩" align="center" prop="minorScore" />
      <el-table-column label="绩点" align="center" prop="gpa" />
      <el-table-column label="成绩标志" align="center" prop="gradeFlag" />
      <el-table-column label="总学时" align="center" prop="totalHours" />
      <el-table-column label="学分" align="center" prop="credit" />
      <el-table-column label="开课单位" align="center" prop="department" />
      <el-table-column label="开课系" align="center" prop="faculty" />
      <el-table-column label="课程性质" align="center" prop="courseType" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['system:grade:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['system:grade:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改课程成绩对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="gradeRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="学年学期" prop="academicYear">
          <el-input v-model="form.academicYear" placeholder="请输入学年学期" />
        </el-form-item>
        <el-form-item label="补重学期" prop="retakeSemester">
          <el-input v-model="form.retakeSemester" placeholder="请输入补重学期" />
        </el-form-item>
        <el-form-item label="课程编号" prop="courseCode">
          <el-input v-model="form.courseCode" placeholder="请输入课程编号" />
        </el-form-item>
        <el-form-item label="课程名称" prop="courseName">
          <el-input v-model="form.courseName" placeholder="请输入课程名称" />
        </el-form-item>
        <el-form-item label="总成绩" prop="totalScore">
          <el-input v-model="form.totalScore" placeholder="请输入总成绩" />
        </el-form-item>
        <el-form-item label="平时成绩" prop="regularScore">
          <el-input v-model="form.regularScore" placeholder="请输入平时成绩" />
        </el-form-item>
        <el-form-item label="期中成绩" prop="midtermScore">
          <el-input v-model="form.midtermScore" placeholder="请输入期中成绩" />
        </el-form-item>
        <el-form-item label="实验成绩" prop="labScore">
          <el-input v-model="form.labScore" placeholder="请输入实验成绩" />
        </el-form-item>
        <el-form-item label="期末成绩" prop="finalScore">
          <el-input v-model="form.finalScore" placeholder="请输入期末成绩" />
        </el-form-item>
        <el-form-item label="辅修成绩" prop="minorScore">
          <el-input v-model="form.minorScore" placeholder="请输入辅修成绩" />
        </el-form-item>
        <el-form-item label="绩点" prop="gpa">
          <el-input v-model="form.gpa" placeholder="请输入绩点" />
        </el-form-item>
        <el-form-item label="成绩标志" prop="gradeFlag">
          <el-input v-model="form.gradeFlag" placeholder="请输入成绩标志" />
        </el-form-item>
        <el-form-item label="总学时" prop="totalHours">
          <el-input v-model="form.totalHours" placeholder="请输入总学时" />
        </el-form-item>
        <el-form-item label="学分" prop="credit">
          <el-input v-model="form.credit" placeholder="请输入学分" />
        </el-form-item>
        <el-form-item label="开课单位" prop="department">
          <el-input v-model="form.department" placeholder="请输入开课单位" />
        </el-form-item>
        <el-form-item label="开课系" prop="faculty">
          <el-input v-model="form.faculty" placeholder="请输入开课系" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Grade">
import { listGrade, getGrade, delGrade, addGrade, updateGrade } from "@/api/system/grade";

const { proxy } = getCurrentInstance();

const gradeList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    academicYear: null,
    retakeSemester: null,
    courseCode: null,
    courseName: null,
    totalScore: null,
    regularScore: null,
    midtermScore: null,
    labScore: null,
    finalScore: null,
    minorScore: null,
    gpa: null,
    gradeFlag: null,
    totalHours: null,
    credit: null,
    department: null,
    faculty: null,
    courseType: null
  },
  rules: {
    academicYear: [
      { required: true, message: "学年学期不能为空", trigger: "blur" }
    ],
    courseCode: [
      { required: true, message: "课程编号不能为空", trigger: "blur" }
    ],
    courseName: [
      { required: true, message: "课程名称不能为空", trigger: "blur" }
    ],
    totalScore: [
      { required: true, message: "总成绩不能为空", trigger: "blur" }
    ],
    finalScore: [
      { required: true, message: "期末成绩不能为空", trigger: "blur" }
    ],
    gpa: [
      { required: true, message: "绩点不能为空", trigger: "blur" }
    ],
    gradeFlag: [
      { required: true, message: "成绩标志不能为空", trigger: "blur" }
    ],
    totalHours: [
      { required: true, message: "总学时不能为空", trigger: "blur" }
    ],
    credit: [
      { required: true, message: "学分不能为空", trigger: "blur" }
    ],
    department: [
      { required: true, message: "开课单位不能为空", trigger: "blur" }
    ],
    faculty: [
      { required: true, message: "开课系不能为空", trigger: "blur" }
    ],
    courseType: [
      { required: true, message: "课程性质不能为空", trigger: "change" }
    ]
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询课程成绩列表 */
function getList() {
  loading.value = true;
  listGrade(queryParams.value).then(response => {
    gradeList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    courseId: null,
    academicYear: null,
    retakeSemester: null,
    courseCode: null,
    courseName: null,
    totalScore: null,
    regularScore: null,
    midtermScore: null,
    labScore: null,
    finalScore: null,
    minorScore: null,
    gpa: null,
    gradeFlag: null,
    totalHours: null,
    credit: null,
    department: null,
    faculty: null,
    courseType: null
  };
  proxy.resetForm("gradeRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.courseId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加课程成绩";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _courseId = row.courseId || ids.value
  getGrade(_courseId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改课程成绩";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["gradeRef"].validate(valid => {
    if (valid) {
      if (form.value.courseId != null) {
        updateGrade(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addGrade(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _courseIds = row.courseId || ids.value;
  proxy.$modal.confirm('是否确认删除课程成绩编号为"' + _courseIds + '"的数据项？').then(function() {
    return delGrade(_courseIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('system/grade/export', {
    ...queryParams.value
  }, `grade_${new Date().getTime()}.xlsx`)
}

getList();
</script>
